package com.etc.zmall.dao.impl;

import java.util.List;

import com.etc.util.DBUtil;
import com.etc.util.PageData;
import com.etc.zmall.dao.UserDao;
import com.etc.zmall.entity.User;

/**
 * @author Administrator shift+alt+j
 *
 */
public class UserDaoImpl implements UserDao {

	@Override
	public User userLogin(String username, String userpwd) {
		String sql = "select * from tbl_user where username=? and userpwd=? and userstate=1";
		List<User> list = (List<User>) DBUtil.exQuery(sql, User.class, username, userpwd);
		// list里有记录，登录成功
		if (list.size() > 0) {
			return list.get(0);
		}
		// 登录失败
		return null;
	}

	/**
	 * 新增用户
	 * 
	 * @param user User对象
	 * @return true操作成功 false 操作失败
	 */
	@Override
	public boolean addUser(User user) {
		String sql = "insert into tbl_user values(null,?,?,?,?,?)";
		return DBUtil.exUpdate(sql, user.getUsername(), user.getUserpwd(), user.getUserphone(), user.getUserstate(),
				user.getAddress());
	}

	/**
	 * 用户状态修改成功
	 */
	@Override
	public boolean proUser(String username, int userstate) {
		String sql = "update tbl_user set userstate=? where username=?";
		return DBUtil.exUpdate(sql, userstate, username);

	}

	/**
	 * 删除被封禁的用户
	 */
	@Override
	public boolean delUser() {
		String sql = "delete from tbl_user where userstate=0";
		return DBUtil.exUpdate(sql);

	}

	@Override
	public PageData<User> selUserByPage(int page, int pageSize, String key) {
		String sql = "select * from tbl_user where username like ?";
		PageData<User> pd = DBUtil.exQueryByPage(sql, User.class, page, pageSize, "%" + key + "%");
		return pd;
	}

	/**
	 *
	 */
	@Override
	public boolean modUser(int uid, String username, String userpwd, String userphone, String address) {
		String sql = "update tbl_user set username=?,userpwd=?,userphone=?,address=? where userid=?";
		return DBUtil.exUpdate(sql, username, userpwd, userphone, address, uid);

	}

	/**
	 * 
	 */

	@Override
	public User getUserByName(String username) {
		// TODO Auto-generated method stub
		String sql = "select * from tbl_user where username = ?";
		List<User> list = (List<User>) DBUtil.exQuery(sql, User.class, username);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<User> getUser(String keywords) {
		// TODO Auto-generated method stub
		String sql = "select * from tbl_user where username like ?";
		List<User> list = (List<User>) DBUtil.exQuery(sql, User.class, "%" + keywords + "%");
		return list;
	}

}
